package Controllers;

import Connection.User;
import Entities.Ball;
import Entities.Bar;
import Entities.Phase;
import SwingComponents.ArkanoidPanel;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class PhaseController implements KeyListener {

    private Phase phase;
    private ArkanoidController arkanoidController;
    private ArkanoidPanel arkanoidPanel;
    private Bar bar;
    private Ball ball;
    private AnimationController animationController;
    private Renderer renderer;
    private User user;

    public PhaseController(Phase phase, ArkanoidController arkanoidController, User user) {
        this.phase = phase;
        this.arkanoidController = arkanoidController;
        arkanoidPanel = arkanoidController.getArkanoidPanel();
        arkanoidPanel.addKeyListener(this);
        arkanoidPanel.getCanvas().addKeyListener(this);

        bar = new Bar(220, 520);
        ball = new Ball(220, 500, 0, 0, 8);

        this.user = user;
    }

    public void execute() {
        phase.addChild(ball);
        phase.addChild(bar);

        if (user != null && user.getGameState() != null) {
            renderer = new Renderer(arkanoidController.getPrincipalController().getApplication(), arkanoidPanel, phase, user.getGameState());
        } else {
            renderer = new Renderer(arkanoidController.getPrincipalController().getApplication(), arkanoidPanel, phase, user.getGameState());
        }
        animationController = new AnimationController(phase, bar, ball, 465, this, user);

        renderer.start();
        animationController.start();
    }

    public void setPhase(Phase phase) {
        this.phase = phase;
    }

    public void finish() {
        renderer.setTerminar(true);
        animationController.setTerminar(true);
        arkanoidController.phaseFinished(this);
    }

    public void gameOver() {
        renderer.setTerminar(true);
        animationController.setTerminar(true);
        arkanoidController.gameOver();
    }

    @Override
    public void keyTyped(KeyEvent e) {
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            animationController.setRigthPressed(true);
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            animationController.setLeftPressed(true);
        } else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            animationController.setSpacePressed(true);
        } else if (e.getKeyCode() == KeyEvent.VK_P) {
            if (user.getGameState().getGamePaused()) {
                animationController.setTerminar(Boolean.FALSE);
                renderer.setTerminar(Boolean.FALSE);
                animationController.start();
                renderer.start();
                user.getGameState().setGamePaused(Boolean.FALSE);
            } else {
                animationController.setTerminar(Boolean.TRUE);
                renderer.setTerminar(Boolean.TRUE);
                user.getGameState().setGamePaused(Boolean.TRUE);
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            animationController.setRigthPressed(false);
        } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            animationController.setLeftPressed(false);
        } else if (e.getKeyCode() == KeyEvent.VK_SPACE) {
            animationController.setSpacePressed(false);
        }
    }

    public void ballLost() {
        arkanoidController.ballLost();
    }
}
